package com.fzq.server.mapper.impl;

import com.fzq.server.mapper.MinioService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author fzq
 * @date 2025/4/22 - 14:09
 **/
@Slf4j
@Component
public class MinioServiceImpl implements MinioService {

    @Resource
    private MinioClient minioClient;

    /**
     * 直接上传到minio
     * @param bucketName
     * @param file
     */
    @Override
    public void putminio(String bucketName, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        long size = file.getSize();
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(originalFilename)
                            .stream(file.getInputStream(),size,-1)
                            .contentType(contentType)
                            .build()
            );
        }catch (Exception e){
            e.printStackTrace();
            log.info("上传minio异常： " + e.getMessage() );
        }
    }


    /**
     * 先获取本地文件再上传到minio
     * @param bucketName
     */
    @Override
    public void putminiotest(String bucketName) {



    }
}
